package com.jxw.cidiansdkdemo;

import android.content.ComponentName;
import android.content.Context;

import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.jxw.cidian.DicActiveCallback;
import com.jxw.cidian.IJwarService;
import com.jxw.video.VideoActiveCallback;


//Jwar引擎封装
public class JwarWrapper {

	private static final String TAG = "PlatSignatureWrapper";
	OnVerify mCallback;
	public interface OnVerify {
		void ActiveDicResult(boolean success);
		void ActiveJwarResult(boolean success);
		void onError(boolean success);
	}
	
	private Context mContext;
	private IJwarService mJwarService;
	private ServiceConnection mJhtServiceConnection;
	
	public JwarWrapper(Context context) {
		mContext = context;
	}
	
	public void start(OnVerify cb) {
		mCallback = cb;
		mJhtServiceConnection = new ServiceConnection() {
		    // Called when the connection with the service is established
		    public void onServiceConnected(ComponentName className, IBinder service) {
		        // Following the example above for an AIDL interface,
		        // this gets an instance of the IRemoteInterface, which we can use to call on the service
		    	synchronized (this) {
					mJwarService = IJwarService.Stub.asInterface(service);
		    	}
				mCallback.onError(true);
		    }
		    
		    // Called when the connection with the service disconnects unexpectedly
		    public void onServiceDisconnected(ComponentName className) {
		    	synchronized (this) {
			 		mCallback.onError(false);
					mJwarService = null;
		    	}
		    }
		};
		Intent intent = new Intent();
		intent.setClassName("com.jxw.zncd", "com.jxw.cidian.JwarService");
		if (!mContext.bindService(intent, mJhtServiceConnection, Context.BIND_AUTO_CREATE)) {
			mCallback.onError(false);
		}
	}
	
	public void release() {
		synchronized (this) {
			if (mJwarService != null)
				try {
					mJwarService.unregisterCallback(mActiveCallback);
				} catch (Exception e) {
					e.printStackTrace();
				}
			if (mJwarService != null) {
				try {
					mContext.unbindService(mJhtServiceConnection);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
				mJwarService = null;
			}
		}
	}

	/**
	 *打开学王词典引擎
	 * @param path 学王词典路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openXWCD(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openXWCD(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 英语单词搜索
	 * @param word 搜索单词
	 * @return 搜索结果
	 */
	public String searchWord(String word){
		if(mJwarService!=null){
			try {
				return mJwarService.searchWord(word);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取学王词典英语单词英式音频
	 * @param peng  英式音频ID
	 * @return 音频数据
	 */
	public byte[] getPEngVoice(int peng){
		if(mJwarService!=null){
			try {
				return mJwarService.getPEngVoice(peng);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取学王词典英语单词美式音频
	 * @param pusa  美式音频ID
	 * @return 音频数据
	 */
	public byte[] getPUsaVoice(int pusa){
		if(mJwarService!=null){
			try {
				return mJwarService.getPUsaVoice(pusa);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取学王词典英语单词自然拼读音频
	 * @param pind  自然拼读音频ID
	 * @return 音频数据
	 */
	public byte[] getPindVoice(int pind){
		if(mJwarService!=null){
			try {
				return mJwarService.getPindVoice(pind);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取学王词典英语例句音频
	 * @param exmp  例句音频ID
	 * @return 音频数据
	 */
	public byte[] getExmpVoice(int exmp){
		if(mJwarService!=null){
			try {
				return mJwarService.getExmpVoice(exmp);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取学王词典单词gif图
	 * @param gif  gif图片ID
	 * @return gif图片数据
	 */
	public byte[] getGifImg(int gif){
		if(mJwarService!=null){
			try {
				return mJwarService.getGifImg(gif);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭学王词典
	 */
	public void closeXWCDFile( ){
		if(mJwarService!=null){
			try {
				 mJwarService.closeXWCDFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *打开汉语字典引擎
	 * @param path 汉语字典路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openHYZDFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openHYZDFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 汉语字典汉字搜索
	 * @param keyword 搜索汉字
	 * @return 搜索结果
	 */
	public String searchHYZD(String keyword){
		if(mJwarService!=null){
			try {
				return mJwarService.searchHYZD(keyword);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取汉语字典汉字音频
	 * @param voice  汉字音频ID
	 * @return 音频数据
	 */
	public byte[] getHZVoice(int voice){
		if(mJwarService!=null){
			try {
				return mJwarService.getHZVoice(voice);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取汉语字典笔画图片
	 * @param bhtp  汉字笔画图片ID
	 * @return 笔画图片数据
	 */
	public byte[] getBhtpImg(int bhtp){
		if(mJwarService!=null){
			try {
				return mJwarService.getBhtpImg(bhtp);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭汉语字典
	 */
	public void closeHYZDFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closeHYZDFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *
	 * @param path 汉语词典路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openHYCDFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openHYCDFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 汉语词典搜索
	 * @param keyword 搜索词语
	 * @return 搜索结果
	 */
	public String searchHYCD(String keyword){
		if(mJwarService!=null){
			try {
				return mJwarService.searchHYCD(keyword);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭汉语词典
	 */
	public void closeHYCDFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closeHYCDFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 打开拼音库引擎
	 * @param path 拼音库路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openPYFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openPYFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 拼音库 拼音搜索
	 * @param pinyin 搜索拼音
	 * @return 搜索结果
	 */
	public String searchPinYin(String pinyin){
		if(mJwarService!=null){
			try {
				return mJwarService.searchPinYin(pinyin);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取拼音库拼读音频
	 * @param pdno  拼读音频ID
	 * @return 音频数据
	 */
	public byte[] getPYPDVoice(int pdno){
		if(mJwarService!=null){
			try {
				return mJwarService.getPYPDVoice(pdno);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取拼音库整读音频
	 * @param zhdno  整读音频ID
	 * @return 音频数据
	 */
	public byte[] getPYZDVoice(int zhdno){
		if(mJwarService!=null){
			try {
				return mJwarService.getPYZDVoice(zhdno);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭拼音库
	 */
	public void closePYFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closePYFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *
	 * @param path 汉英大词典路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openHYDCDFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openHYDCDFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 汉英大词典搜索
	 * @param keyword 搜索汉语
	 * @return 搜索结果
	 */
	public String searchHYDCD(String keyword){
		if(mJwarService!=null){
			try {
				return mJwarService.searchHYDCD(keyword);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭汉英大词典
	 */
	public void closeHYDCDFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closeHYDCDFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 打开成语词典引擎
	 * @param path 成语词典路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openCYCDFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openCYCDFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 成语词典 成语搜索
	 * @param keyword 搜索成语
	 * @return 搜索结果
	 */
	public String searchChengYu(String keyword){
		if(mJwarService!=null){
			try {
				return mJwarService.searchChengYu(keyword);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取成语词典成语音频
	 * @param sid  成语音频ID
	 * @return 音频数据
	 */
	public byte[] getCYVoice(int sid){
		if(mJwarService!=null){
			try {
				return mJwarService.getCYVoice(sid);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭成语词典
	 */
	public void closeCYCDFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closeCYCDFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *
	 * @param path 古汉语字典路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openGHYZDFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openGHYZDFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 古汉语字搜索
	 * @param keyword 搜索古汉字
	 * @return 搜索结果
	 */
	public String searchGHY(String keyword){
		if(mJwarService!=null){
			try {
				return mJwarService.searchGHY(keyword);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取古汉语字典古汉字音频
	 * @param sid  古汉字音频ID
	 * @return 音频数据
	 */
	public byte[] getGHYVoice(int sid){
		if(mJwarService!=null){
			try {
				return mJwarService.getGHYVoice(sid);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取古汉语字典古汉字图
	 * @param pid  古汉字图ID
	 * @return 图片数据
	 */
	public byte[] getGHYIMG(int pid){
		if(mJwarService!=null){
			try {
				return mJwarService.getGHYIMG(pid);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭古汉语字典
	 */
	public void closeGHYZDFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closeGHYZDFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 打开古诗文词典引擎
	 * @param path 古诗文词典路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openGSWCDFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openGSWCDFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 古诗文搜索
	 * @param gsw 搜索关键字
	 * @return 搜索结果
	 */
	public String searchGSW(String gsw){
		if(mJwarService!=null){
			try {
				return mJwarService.searchGSW(gsw);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取古诗文词典原文标题音频
	 * @param sid  原文标题音频ID
	 * @return 音频数据
	 */
	public byte[] getGSWYWVoice(int sid){
		if(mJwarService!=null){
			try {
				return mJwarService.getGSWYWVoice(sid);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取古诗文词典作者音频
	 * @param sid  作者音频ID
	 * @return 音频数据
	 */
	public byte[] getGSWZZVoice(int sid){
		if(mJwarService!=null){
			try {
				return mJwarService.getGSWZZVoice(sid);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取古诗文词典朝代音频
	 * @param sid  朝代音频ID
	 * @return 音频数据
	 */
	public byte[] getGSWCDVoice(int sid){
		if(mJwarService!=null){
			try {
				return mJwarService.getGSWCDVoice(sid);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 关闭古诗文词典
	 */
	public void closeGSWCDFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closeGSWCDFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 *
	 * @param path 课本路径
	 * @return true表示打开成功   false打开失败
	 */
	public boolean openKBFile(String path){
		if(mJwarService!=null){
			try {
				return mJwarService.openKBFile(path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 关闭课本
	 */
	public void closeKBFile( ){
		if(mJwarService!=null){
			try {
				mJwarService.closeKBFile();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取课本页码奇偶属性
	 * @return (0无，1奇，2偶)
	 */
	public int getKBPageOdd(){
		if(mJwarService!=null){
			try {
				return mJwarService.getKBPageOdd();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	/**
	 *获取课本页码总数
	 * @return 当前课本总页数（双页）
	 */
	public int getKBPageTotal(){
		if(mJwarService!=null){
			try {
				return mJwarService.getKBPageTotal();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	/**
	 *获取课本指定页码整读数据
	 * @param iPage  页码
	 * @return 整读数据
	 */
	public byte[] getKBPageContent(int iPage){
		if(mJwarService!=null){
			try {
				return mJwarService.getKBPageContent(iPage);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 *获取课本指定页码讲解数据
	 * @param iPage  页码
	 * @return 讲解数据
	 */
	public byte[] getKBPageJJContent(int iPage){
		if(mJwarService!=null){
			try {
				return mJwarService.getKBPageJJContent(iPage);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 *获取课本音频数据
	 * @param soundId  音频ID
	 * @return 音频数据
	 */
	public byte[] getKBVoice(int soundId){
		if(mJwarService!=null){
			try {
				return mJwarService.getKBVoice(soundId);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取单元目录信息
	 * @param isWordUnit 是否是单词单元目录  false 整读  true 单词
	 * @return {"name":"UNIT 1",
	 * "list":[{"p":1,"n":6},{"p":2,"n":0},{"p":2,"n":1},{"p":2,"n":2}],"zwNum":4})
	 */
	public String getKBUnits(boolean isWordUnit){
		if(mJwarService!=null){
			try {
				return mJwarService.getKBUnits(isWordUnit);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取单元数据
	 * @param pList 单元信息集合 getKBUnits返回的list数据
	 *              [{"p":1,"n":6},{"p":2,"n":0},{"p":2,"n":1},{"p":2,"n":2}]
	 * @param isWordUnit 是否是单词单元目录  false 整读  true 单词
	 * @return [{"yn":116,"yc":"pencil","zn":125,"zc":"\u94c5\u7b14"},
	 * {"yn":117,"yc":"new","zn":126,"zc":"\u65b0\u7684"},
	 * {"yn":139,"yc":"eraser","zn":150,"zc":"\u6a61\u76ae"}]
	 */
	public String getUnitContent(String pList,boolean isWordUnit){
		if(mJwarService!=null){
			try {
				return mJwarService.getUnitContent(pList,isWordUnit);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 书本单元词头数据
	 * @param book_path 书本路径
	 * @return  单元词头数据
	 */
	public String JxwBookInfoToJsonStr(String book_path){
		if(mJwarService!=null){
			try {
				return mJwarService.JxwBookInfoToJsonStr(book_path);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 模糊搜索英文单词
	 * @param word 匹配字母
	 * @param maxCount 返回的最大匹配个数  支持范围 1-50
	 * @return 匹配单词 当查询结果为1个时，直接返回单词详情，否者返回匹配的单词，查询单词详情可以通过searchWord接口
	 */
	public String matchWord(String word,int maxCount){
		if(mJwarService!=null){
			try {
				return mJwarService.matchWord(word,maxCount);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 汉字反查英文单词
	 * @param hanzi 查询汉字
	 * @return 反查结果
	 */
	public String inverseWord(String hanzi){
		if(mJwarService!=null){
			try {
				return mJwarService.inverseWord(hanzi);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		return null;
	}


	public DicActiveCallback mActiveCallback = new DicActiveCallback.Stub() {


		@Override
		public void ActiveDicResult(boolean success) throws RemoteException {
			if(mCallback!=null){
				mCallback.ActiveDicResult(success);
			}
		}

		@Override
		public void ActiveJwarResult(boolean success) throws RemoteException {
			if(mCallback!=null){
				mCallback.ActiveJwarResult(success);
			}
		}
	};


	public void ActiveDic(){
		if(mJwarService!=null){
			try {
				 mJwarService.ActiveDic();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}


	public void ActiveJwar(){
		if(mJwarService!=null){
			try {
				mJwarService.ActiveJwar();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}
}